/**
 * @Author: douxuefeng
 * @Date: 2020/12/2 下午2:48
 * @File: oauth_token_repository
 * @Desc:
 */
package repository

import (
	comm "gitee.com/douxuefeng/go-common"
	"gitee.com/douxuefeng/go-iris/model"
)

type oauthTokenRespository struct {

}

func newOauthTokenRepository() *oauthTokenRespository {
	return &oauthTokenRespository{}
}
var OauthTokenRepository = newOauthTokenRepository()

func (r *oauthTokenRespository) CheckToken(uuid, source string, userId int64) bool {
	err := comm.GetDb().First(&model.OauthToken{}, "id=? and source=? and user_id=?", uuid, source,userId).Error
	if err != nil {
		return false
	}
	return true
}
func (r *oauthTokenRespository) StoreToken(uuid, source string, userId int64) error {
	db := comm.GetDb()
	return db.Create(&model.OauthToken{Id: uuid, Source: source, UserId: userId}).Error
}

func (r *oauthTokenRespository) DeleteToken(userId int64, source string) error {
	return comm.GetDb().Where("source =? and user_id=?", source, userId).Delete(&model.OauthToken{}).Error
}